var sdui={
  "datetime":"202405260800",
  "startday":"1",
  "minTemp24h":"2870",
  "maxTemp24h":"2980",
  "retTmp03Band":"6"
};

function main() { 
    let startday=parseInt(sdui.startday);
    let dt=Number( sdui.datetime );
    if( startday>=1 && startday<=10 )
    {
        let tminName="CMA/TMIN/B"+ String(1000+startday).substr(1);
        let tmaxName="CMA/TMAX/B"+ String(1000+startday).substr(1);
        
        let dsMin = pe.Dataset(tminName, dt);
        let dsMax = pe.Dataset(tmaxName, dt);
        
        let temMin24h = Number(sdui.minTemp24h);
        let temMax24h = Number(sdui.maxTemp24h);
        
        let startT03 = (startday-1)*8+1;
        let endT03 = startday*8 ;
        let dsT03Arr = [] ;
        for( let i3=startT03; i3<=endT03; ++ i3 ) {
            let tName="CMA/TMP03/B"+ String(1000+i3).substr(1);
            let tDs=pe.Dataset(tName,dt) ;
            dsT03Arr.push( tDs );
        }
        let ds03Min = pe.compose2(dsT03Arr,
            pe.CompositeMethodMin, 1, 32700, 0, 3);
        let ds03Max = pe.compose2(dsT03Arr,
            pe.CompositeMethodMax, 1, 32700, 0, 3);
        
        let retBandIndex = parseInt( sdui.retTmp03Band );
        let retDs = pe.Dataset(
            "CMA/TMP03/B"+ String(1000+retBandIndex).substr(1),
            dt
            );
        retDs=retDs.forEachPixel(function(va,ind){
            let smin = ds03Min.tiledata[ind];
            let smax = ds03Max.tiledata[ind];
            if( smin===smax ) {
                return 0;
            }else{
                let scale= (temMax24h - temMin24h)/(smax-smin) ;
                let offset=temMin24h - scale*smin;
                let adjVal=scale*va[0]+offset;
                return Math.min( temMax24h , Math.max(temMin24h, adjVal) );   
            }
        });
        return retDs; 
    }else{
        return null;
    }
}